using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using System.Collections;
using PureSystems.Modules.psSquared.Components;
using System.Reflection;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security.Roles;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;

namespace PureSystems.Modules.psSquared
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages Module Settings
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Settings : ModuleSettingsBase
    {

        #region Base Method Implementations

        string strExlcudedRoles;

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    //Check for existing settings and use those on this page
                    tbxAccount.Text = (string)ModuleSettings["GoSquaredAcc"];
                    strExlcudedRoles = (string)ModuleSettings["ExcludedRoles"];

                    LoadRoles();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void UpdateSettings()
        {
            try
            {
                ModuleController modules = new ModuleController();
                modules.UpdateModuleSetting(this.ModuleId, "GoSquaredAcc", tbxAccount.Text);

                string roleSelections = "";
                foreach (ListItem li in cblDNNRoles.Items)
                {
                    if (li.Selected)
                    {
                        roleSelections += li.Value + ";";
                    }
                }
                if (roleSelections.Length > 0)
                {
                    roleSelections = roleSelections.Substring(0, roleSelections.Length - 1);
                }

                modules.UpdateModuleSetting(this.ModuleId, "ExcludedRoles", roleSelections);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void LoadRoles()
        {
            cblDNNRoles.Items.Clear();

            ArrayList colPortalRoles = new RoleController().GetPortalRoles(PortalId);
            cblDNNRoles.DataSource = colPortalRoles;
            cblDNNRoles.DataTextField = "RoleName";
            cblDNNRoles.DataValueField = "RoleName";
            cblDNNRoles.DataBind();
        }

        protected void cblDNNRoles_DataBound(object sender, EventArgs e)
        {
            // Get the selected item for the DNN Roles and select
            if (strExlcudedRoles != null)
            {
                ListItem objSelectedListItem = new ListItem();

                string[] arrExcludedRoles = strExlcudedRoles.Split(';');
                foreach (string excludedRole in arrExcludedRoles)
                {

                    foreach (ListItem li in cblDNNRoles.Items)
                    {
                        if (li.Value == excludedRole)
                        {
                            li.Selected = true;
                        }
                    }

                }
            }
        }

        #endregion

    }

}

